package com.turbo.cloud.ums.web.webservice;

import com.turbo.cloud.core.api.R;
import com.turbo.cloud.core.api.Result;
import com.turbo.cloud.ums.captcha.HappyCaptcha;
import com.turbo.cloud.ums.captcha.support.CaptchaType;
import com.turbo.cloud.ums.utils.HappyCaptchaRequestUtil;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;



/**
 * 图形验证码表 前端控制器
 *
 * @author zhangluning
 */
@Slf4j
@RequiredArgsConstructor
@RestController
public class HappyCaptchaController {


    /**
     * 生成登录图形验证码
     * <p>
     * 值	说明
     * DEFAULT	            数字、大小写字母随机组合
     * ARITHMETIC	        加、减、乘算数运算表达式
     * ARITHMETIC_ZH        中文简体加、减、乘算数运算表达式描述
     * CHINESE	            常见汉字（3500个）随机组合
     * NUMBER	            0~9数字随机组合
     * NUMBER_ZH_CN	        中文数字（零至九）随机组合
     * NUMBER_ZH_HK	        中文繁体数字（零至玖）随机组合
     * WORD	                大小写字母随机组合
     * WORD_LOWER	        小写字母随机组合
     * WORD_UPPER	        大写字母随机组合
     * WORD_NUMBER_LOWER	数字、小写字母随机组合
     * WORD_NUMBER_UPPER	数字、大写字母随机组合
     * <p>
     * HappyCaptcha.require(request,response).type(CaptchaType.CHINESE).build().finish();
     * <p>
     * 可供选择的值有IMG和ANIM
     * HappyCaptcha.require(request,response).style(CaptchaStyle.ANIM).build().finish();
     * <p>
     * <a href="https://gitee.com/ramostear/Happy-Captcha/blob/master/README.md">Happy-Captcha(Gitee)</a>
     */
    @GetMapping(name = "生成登录图形验证码", value = "/admin/captcha/getCaptchaImage")
    public void getCaptchaImage(HttpServletRequest request, HttpServletResponse response) {
        HappyCaptcha.require(request, response).type(CaptchaType.ARITHMETIC).build().finish();
    }

    /**
     * 是否显示登录图像验证码
     */
    @GetMapping(name = "是否显示登录图像验证码", value = "/admin/captcha/showCaptchaImage")
    public Result<Boolean> displayCaptchaImage(HttpServletRequest request) {
        Boolean isShow = HappyCaptchaRequestUtil.showHappyCaptcha(request);
        return R.result(isShow);
    }
}
